var alarms = [];
var undefinedReplace = '空';
var start = $('#startTime').val();
var end = $('#endTime').val();
var stationId = $('#stationId').val();
var alarmTable;
var lines = [];
var stations = [];
var voltages = [];
var reasons = [];
var columns = [];
var lineMap = {};
var stationMap = {};
var lineSelectedMap = {};
var stationSelectedMap = {};
var languageUrl;
var columnMap = {
	0 : locale.XuHao,
	1 : locale.ChangZhanMingCheng,
	2 : locale.XianLu,
	3 : locale.BaoHuMingCheng,
	4 : locale.DianYaDengJi,
	5 : locale.GaoJingShiJian,
	6 : locale.ZhongDuanShiChang,
	7 : locale.XiaoXiNeiRong
};
function init() {
	if ($('#start').val() == null || $('#start').val() == "") {
		$('#start').val($('#startTime').val());
		$('#end').val($('#endTime').val());
	}
		$('#stationId').val($('#stationId').val());
	$('#start').datetimepicker({
		format : 'yyyy-mm-dd',
		minView : 2,
		autoclose : true,
		endDate : new Date(),
		todayHighlight : true
	});
	$('#end').datetimepicker({
		format : 'yyyy-mm-dd',
		minView : 2,
		autoclose : true,
		endDate : new Date(),
		pickerPosition : 'bottom-left',
		todayHighlight : true
	});
	loadTableData();
	loadTableFilter();
}
function formatTime(v) {
	if (!v) {
		return '';
	} else {
		var timeTrans = new Date(parseInt(v.time ? v.time : v));
		return timeTrans.toLocaleString('chinese', {
			hour12 : false
		});
	}
}
function loadTableData() {
	if (alarmTable) {
		alarmTable.destroy();
	}
	if (typeof (systemLanguage) != 'undefined' && systemLanguage && systemLanguage == 'en') {
		languageUrl = "";
	} else {
		languageUrl = "/resources/plugins/datatables/Chinese.json";
	}
	var date = new Date();
	var result;
	alarmTable = $("#alarmTable").DataTable({
		"bLengthChange" : false,
		"bFilter" : false,
		"bSort" : false,
		"iDisplayLength" : 20,
		"bProcessing" : true,
		"bServerSide" : true,
		"sAjaxSource" : "/rest/protectAlarm/getProtectInterruptAlarmDetails",
		"sServerMethod" : "GET",
		"PaginationType" : "full_numbers",
		"aoColumns" : [ {
			"mDataProp" : "sort",
			title : locale.XuHao,
			"sDefaultContent" : "", // 此列默认值为""，以防数据中没有此值，DataTables加载数据的时候报错
			"sWidth" : "3%",
		}, {
			"mDataProp" : "stationName",
			title : locale.ChangZhanMingCheng,
			"sDefaultContent" : "", // 此列默认值为""，以防数据中没有此值，DataTables加载数据的时候报错
			"sWidth" : "7%",
		}, {
			"mDataProp" : "lineName",
			title : locale.XianLu,
			"sDefaultContent" : "",
			"sWidth" : "7%",
		}, {
			"mDataProp" : "protectName",
			title : locale.BaoHuMingCheng,
			"sDefaultContent" : "",
			"sWidth" : "20%",
		}, {
			"mDataProp" : "voltage",
			title : locale.DianYaDengJi + "(kv)",
			"sDefaultContent" : "",
			"sWidth" : "5%",
		}, {
			"mDataProp" : "eventStart",
			title : locale.GaoJingShiJian,
			"sDefaultContent" : "",
			"sWidth" : "10%",
		}, {
			"mDataProp" : "durationTime",
			title : locale.ZhongDuanShiChang + "(ms)",
			"sDefaultContent" : "",
			"sWidth" : "5%",
		}, {
			"mDataProp" : "alarmMsg",
			title : locale.XiaoXiNeiRong,
			"sDefaultContent" : "",
			"sWidth" : "15%",
		} ],
		"fnServerParams" : function(aoData) {
			aoData.push({
				"name" : "stationId",
				"value" : encodeURI(stationId),
			});
			aoData.push({
				"name" : "startTime",
				"value" : encodeURI(start),
			});
			aoData.push({
				"name" : "endTime",
				"value" : encodeURI(end),
			});
			aoData.push({
				"name" : "lines",
				"value" : encodeURI(lines),
			});
			aoData.push({
				"name" : "stations",
				"value" : encodeURI(stations),
			});
			aoData.push({
				"name" : "voltages",
				"value" : encodeURI(voltages),
			});
			aoData.push({
				"name" : "reasons",
				"value" : encodeURI(reasons),
			});
		},
		"fnServerData" : function(sSource, aoData, fnCallback) {
			$.getJSON(sSource + '/'+encodeURI(JSON.stringify(aoData)), function(result) {
				for (var i = 0; i < result.aaData.length; i++) {
					result.aaData[i].eventStart = formatTime(result.aaData[i].eventStart);
				}
				fnCallback(result);
			});
		},
		"language" : {
			"url" : languageUrl
		},
		"paging" : true,
		"lengthChange" : false,
		"searching" : false,
		"ordering" : false,
		"info" : false,
		"autoWidth" : false
	});
}
function loadTableFilter() {
	if (!stationId || stationId == "null") {
		stationId = "null";
	}
	$.getJSON("/rest/protectAlarm/getFilterLineNames/" + stationId + "/" + start + "/" + end, function(result) {
		lineSelectedMap = result.lineAll;
		stationSelectedMap = result.stationsAll;
		lineMap = result.lines;
		stationMap = result.stations;
		$('#lines').html(getCheckHtml('lines', lineMap));
		$('#stations').html(getCheckHtml('stations', stationMap));
		loadColumns();
	});
	loadColumns();
}
function loadColumns() {
	$('#columns').html(getCheckHtml('columns', columnMap));
	if (columns.length > 0) {
		for (var s = 0; s < 8; s++) {
			alarmTable.column(s).visible(false);
		}
		for (var s = 0; s < columns.length; s++) {
			var num = columns[s];
			alarmTable.column(num).visible(true);
		}
	}

}
function getCheckHtml(name, map) {
	var text = '';
	var allEle = document.getElementById(name + 'All');
	if (allEle.checked) {
		for ( var i in map) {
			var lname = map[i];
			text += '<label><input type="checkbox" checked="checked" onchange="checkboxChange(this,' + name + 'All'
					+ ')" name="' + name + '" value="' + i + '" />' + lname + '</label>';
		}
	} else {
		var list = getObjectList(name); // 如果没有全选，则获取所有选中的值
		for ( var i in map) { // 遍历nameMap,如果map中的值，在选中list中存在，则附上checked，否则checked=false
			var lname = map[i];
			if (list.indexOf(i) > -1) {
				text += '<label><input type="checkbox" checked="checked" onchange="checkboxChange(this,' + name + 'All'
						+ ')" name="' + name + '" value="' + i + '" />' + lname + '</label>';
			} else {
				text += '<label><input type="checkbox"  name="' + name + '" onchange="checkboxChange(this,' + name
						+ 'All' + ')" value="' + i + '" />' + lname + '</label>';
			}
		}
	}
	if (name == 'columns') {
	}
	return text;
}
function checkboxChange(checkbox, allEle) {
	var isAllChecked = true;
	if (!checkbox.checked) {
		allEle.checked = "";
	} else {
		var Ele = document.getElementById((allEle.id).substring(0, (allEle.id).length - 3));
		var objInput = Ele.getElementsByTagName("input");
		for (i = 0; i < objInput.length; i++) {
			if (!objInput[i].checked) {
				isAllChecked = false;
			}
		}
		if (isAllChecked) {
			allEle.checked = "checked";
		}
	}
}
function getObjectList(name) {
	var list = [];
	switch (name) {
	case 'lines':
	list = lines;
		break;
	case 'stations':
	list = stations;
		break;
	case 'voltages':
	list = voltages;
		break;
	case 'reasons':
	list = reasons;
		break;
	case 'columns':
	list = columns;
		break;
	default:
	break;
}
return list;
}
function query() {
var obj1 = document.getElementById('linesAll');
var obj2 = document.getElementById('stationsAll');
if (obj1.checked) {
	lines = [];
} else {
	lines = getCheckBoxValue('lines');
	var s = lines.length - 1;
	if (lines[s] == "qita") {
		for ( var i in lineSelectedMap) {
			lines.push(i);
		}
	}
}
if (obj2.checked) {
	stations = [];
} else {
	stations = getCheckBoxValue('stations');
	var s = stations.length - 1;
	if (stations[s] == "qita") {
		for ( var i in stationSelectedMap) {
			stations.push(i);
		}
	}
}
$('#lines').html('<label>' + locale.JiaZaiZhong + '</label>');
$('#stations').html('<label>' + locale.JiaZaiZhong + '</label>');
var obj3 = document.getElementById('voltagesAll');
if (obj3.checked) {
	voltages = [];
} else {
	voltages = getCheckBoxValue('voltages');
}
reasons = getCheckBoxValue('reasons');
columns = getCheckBoxValue('columns');
var s = $('#start').val();
var e = $('#end').val();
start = s;
end = e;
loadTableData();
loadTableFilter();
}
function getCheckBoxValue(name) {
var obj = document.getElementsByName(name);
var s = [];
for (var i = 0; i < obj.length; i++) {
	if (obj[i].checked) {
		s.push(obj[i].value);
	}
}
return s;
}
function selectAll(isChecked, name) {
var eles = document.getElementsByName(name);
if (isChecked == true) {
	for (var i = 0; i < eles.length; i++) {
		var ele = eles[i];
		ele.checked = "checked";
	}
} else {
	for (var i = 0; i < eles.length; i++) {
		var ele = eles[i];
		ele.checked = "";
	}
}
}
